package com.xy.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xy.shop.common.base.Codes;
import com.xy.shop.common.base.ResponseResult;
import com.xy.shop.common.constant.CommonContants;
import com.xy.shop.common.constant.Rst;
import com.xy.shop.common.dtgrid.model.Pager;
import com.xy.shop.common.utils.EncryptUtils;
import com.xy.shop.web.dto.BaseParam;
import com.xy.shop.web.service.BaseParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zyy
 */
@RequestMapping(value = "/web/config")
@Controller
public class ConfigController extends BaseController {

    @Resource
    private BaseParamService baseParamService;

    @GetMapping(value = "baseParamUi")
    public String getOrderUi() {
        return "config/base_param_list";
    }
    
    /**
    *@Description: 参数设置
    *@Param:  null
    *@Author: zyy
    *@date: 2020/7/1
    *@Time: 16:46
    */
    @ResponseBody
    @RequestMapping(value = "/baseParamList" , method = RequestMethod.POST)
    public Map<String, Object> getOrderList(String gridPager, HttpServletResponse response) {
        List<BaseParam> list;
        Pager pager = JSON.parseObject(gridPager, Pager.class);
        // 判断是否包含自定义参数
        Map<String, Object> parameters = pager.getParameters();
        //getAppParams(parameters);
        Page<Object> page = PageHelper.startPage(pager.getNowPage(), pager.getPageSize());
        // 3、判断是否是导出操作
        // 设置分页，page里面包含了分页信息
        list = baseParamService.queryListByPage(parameters);
        return getReturnPage(pager, page, list);

    }
    @GetMapping(value = "addBaseParamUi")
    public String addBaseParamUi(BaseParam baseParam, Model model) {
        BaseParam baseParam1=null;
        if(Objects.nonNull(baseParam.getId())) {
              baseParam1= baseParamService.selectByPrimaryKey(baseParam.getId());
        }
        model.addAttribute("baseParam",baseParam1);
        return "config/base_param_add";
    }

    @ResponseBody
    @RequestMapping(value = "/addBaseParam" , method = RequestMethod.POST)
    public Map addBaseParam(BaseParam baseParam) {
        int count;
        if(Objects.isNull(baseParam.getId())) {
            count= baseParamService.insertSelective(baseParam);
        }else{
            count= baseParamService.updateByPrimaryKeySelective(baseParam);
        }
        if (count>0) {
        return Rst.getReturnData(null,true,CommonContants.FAILURE_SUCCESS,"添加成功！");
        }else {
            return Rst.getReturnData(null,false,CommonContants.FAILURE_1102,"添加失败！");
        }
    }
    @ResponseBody
    @RequestMapping(value = "/delBaseParam")
    public Map delBaseParam(int id) {
        int count  = baseParamService.deleteByPrimaryKey(id);
        if (count>0) {
            return Rst.getReturnData(null,true,CommonContants.FAILURE_SUCCESS,"删除成功！");
        }else {
            return Rst.getReturnData(null,false,CommonContants.FAILURE_1102,"删除失败！");
        }
    }
}
